package com.ruoyi.legal.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.legal.domain.Question;
import com.ruoyi.legal.mapper.QuestionCategoryMapper;
import com.ruoyi.legal.domain.QuestionCategory;
import com.ruoyi.legal.service.IQuestionCategoryService;

/**
 * 题库分类Service业务层处理
 * 
 * @author xiaoxiao
 * @date 2025-07-01
 */
@Service
public class QuestionCategoryServiceImpl implements IQuestionCategoryService 
{
    @Autowired
    private QuestionCategoryMapper questionCategoryMapper;

    /**
     * 查询题库分类
     * 
     * @param id 题库分类主键
     * @return 题库分类
     */
    @Override
    public QuestionCategory selectQuestionCategoryById(Long id)
    {
        return questionCategoryMapper.selectQuestionCategoryById(id);
    }

    /**
     * 查询题库分类列表
     * 
     * @param questionCategory 题库分类
     * @return 题库分类
     */
    @Override
    public List<QuestionCategory> selectQuestionCategoryList(QuestionCategory questionCategory)
    {
        return questionCategoryMapper.selectQuestionCategoryList(questionCategory);
    }

    /**
     * 新增题库分类
     * 
     * @param questionCategory 题库分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuestionCategory(QuestionCategory questionCategory)
    {
        questionCategory.setCreateTime(DateUtils.getNowDate());
        int rows = questionCategoryMapper.insertQuestionCategory(questionCategory);
        insertQuestion(questionCategory);
        return rows;
    }

    /**
     * 修改题库分类
     * 
     * @param questionCategory 题库分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuestionCategory(QuestionCategory questionCategory)
    {
        questionCategory.setUpdateTime(DateUtils.getNowDate());
        questionCategoryMapper.deleteQuestionByQuestionNo(questionCategory.getId());
        insertQuestion(questionCategory);
        return questionCategoryMapper.updateQuestionCategory(questionCategory);
    }

    /**
     * 批量删除题库分类
     * 
     * @param ids 需要删除的题库分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionCategoryByIds(Long[] ids)
    {
        questionCategoryMapper.deleteQuestionByQuestionNos(ids);
        return questionCategoryMapper.deleteQuestionCategoryByIds(ids);
    }

    /**
     * 删除题库分类信息
     * 
     * @param id 题库分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionCategoryById(Long id)
    {
        questionCategoryMapper.deleteQuestionByQuestionNo(id);
        return questionCategoryMapper.deleteQuestionCategoryById(id);
    }

    /**
     * 新增题目信息
     * 
     * @param questionCategory 题库分类对象
     */
    public void insertQuestion(QuestionCategory questionCategory)
    {
        List<Question> questionList = questionCategory.getQuestionList();
        Long id = questionCategory.getId();
        if (StringUtils.isNotNull(questionList))
        {
            List<Question> list = new ArrayList<Question>();
            for (Question question : questionList)
            {
                question.setQuestionNo(String.valueOf(id));
                list.add(question);
            }
            if (list.size() > 0)
            {
                questionCategoryMapper.batchQuestion(list);
            }
        }
    }
}
